package example.Assignment1;
import jade.core.Agent;
import jade.core.behaviours.Behaviour;
import jade.core.behaviours.SimpleBehaviour;
import jade.lang.acl.ACLMessage;
import org.rosuda.REngine.REXP;
import org.rosuda.REngine.REXPMismatchException;
import org.rosuda.REngine.Rserve.RConnection;
import org.rosuda.REngine.Rserve.RserveException;

import java.io.File;

public class Rec_cluster extends Agent {
    static String kmean_location;
    @Override
    protected void setup() {
        Behaviour b = new SimpleBehaviour() {
            boolean finished = false;
            @Override
            public void action() {
                ACLMessage acl1 = receive();
                if (acl1 != null){
                    System.out.println("receiving");
                    doWait(2000);

                    try {
                        RConnection reconnect = new RConnection("127.0.0.1");
                        REXP x = reconnect.eval("R.version.string");
                        System.out.println(x.asString());

                        File file2 = new File("data2.csv");
                        String[] sd = new String[1];
                        sd[0] = "\\";
//                        reconnect.eval("setwd(\"D:\")");
                        kmean_location = String.valueOf(file2.getAbsoluteFile());
                        kmean_location = detection(kmean_location,sd);
                        reconnect.eval("ddforkmeans <- read.table('" + kmean_location + "', header = T, sep = \",\")");
                        reconnect.eval("ddforkmeans.4means <- kmeans(ddforkmeans, centers = 4)");
                        reconnect.eval("jpeg(file = \"STG_STR.jpeg\")\n" +
                                "plot(dd[c(\"STG\",\"STR\")], col = ddforkmeans.4means$cluster)\n" +
                                "dev.off()\n" +
                                "\n" +
                                "jpeg(file = \"LPR_PEG.jpeg\")\n" +
                                "plot(dd[c(\"LPR\",\"PEG\")], col = ddforkmeans.4means$cluster)\n" +
                                "dev.off()");
                        System.out.println("Finished");
                    }catch (RserveException | REXPMismatchException e) {
                        e.printStackTrace();
                    }

                    finished = true;
                    myAgent.doDelete();
                }else {
                    System.out.println(getLocalName()+" does not receive a message");
                    block();//如果没有消息，则阻塞行为
                }
            }

            @Override
            public boolean done() {
                return finished;
            }
        };
        addBehaviour(b);
    }

    public static String detection(String content,String[] badString){
        for (int i = 0; i <badString.length ; i++) {
            content= index(content,badString[i]);
        }
        return content;
    }

    private static String index(String content,String badString){
        if(content.equals("")||badString.equals("")){
            return content;
        }
        int index = content.indexOf(badString);
        String newString="";
        if(index!=-1){
            String newString1=content.substring(0,index);
            String newString2=content.substring(index+badString.length());
            String hindString="";
            for (int i = 0; i < badString.length() ; i++) {
                hindString = hindString + "//";
            }
            newString = newString1 + hindString + newString2;
            return index(newString,badString);
        }
        return content;
    }
}
